/**
 * Copyright (C), 2015-2021, 开度
 * FileName: UserLoginController
 * Author:   ASUS
 * Date:     2021/4/14 10:59
 * Description: 测试用户登录
 * History:
 * <author>          <time>          <version>          <desc>
 * Hezeyu           2021/4/14           1.0              测试用户登录
 */
package com.example.spring.security.controller;

import com.example.spring.security.interceptor.user.CustomUserDetailsService;
import com.example.spring.security.pojo.UserPojo;
import com.example.spring.security.service.UserInfoServie;
import com.example.spring.security.utils.BaseResponse;
import com.example.spring.security.utils.TokenProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 〈测试用户登录〉
 *
 * @author ASUS
 * @create 2021/4/14
 * @since 1.0.0
 */
@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserInfoServie userInfoServie;

    @Autowired
    private CustomUserDetailsService userDetailsService;

    /**
     * 用户注册
     *
     * @param userPojo
     * @return:com.example.spring.security.spring.utils.BaseResponse
     * @since: 1.0.0
     * @Author:hezeyu
     * @Date: 2021/4/27 14:26
     */
    @PostMapping("/register")
    @ResponseBody
    public BaseResponse doRegister(@RequestBody UserPojo userPojo) throws Exception {
        return userInfoServie.inset(userPojo);
    }

    /**
     * 登录成功回调接口
     *
     * @param authentication
     * @return:java.lang.String
     * @since: 1.0.0
     * @Author:hezeyu
     * @Date: 2021/4/27 14:26
     */
    @RequestMapping("/main")
    public String toMain(Authentication authentication) {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("登录用户：" + name);
        return "index";
    }

    @RequestMapping("/login")
    public String toLogin(Authentication authentication) {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("登录用户：" + name);
        return "login";
    }

    /**
     * 〈登录接口，根据账号密码返回token，并且保持登录信息到session〉
     *
     * @return:java.lang.String
     * @since: 1.0.0
     * @Author:hezeyu
     * @Date: 2021/4/20 14:36
     */
    @RequestMapping(value = "/authenticate")
    public String authorize(HttpServletRequest request, Model model) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        log.info("进入登录方法");
        // 1 创建UsernamePasswordAuthenticationToken
        UsernamePasswordAuthenticationToken token
                = new UsernamePasswordAuthenticationToken(username, password);
        // 2 认证
        Authentication authentication = authenticationManager.authenticate(token);
        // 3 保存认证信息
        SecurityContextHolder.getContext().setAuthentication(authentication);
        // 4 加载UserDetails
        UserDetails details = userDetailsService.loadUserByUsername(username);
        // 5 生成自定义token
        model.addAttribute("token",TokenProvider.token(username, password));
        model.addAttribute("username",username);
        return "user/user";
    }
}
